package br.com.lucasoliveira.modules.comprova;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ComprovaIDFinder {

	private BufferedReader getReader(File arq) throws FileNotFoundException {
		FileReader fr = new FileReader(arq);
		BufferedReader bf = new BufferedReader(fr);
		return bf;
	}

	public String getComprovaEmailID(File arq) throws IOException {
		BufferedReader lerArq = getReader(arq);
		String linha = lerArq.readLine();
		while (linha != null) {
			String id = findEmailID(linha);
			if (id != null) {
				lerArq.close();
				return id;
			} else {
				linha = lerArq.readLine();
			}
		}
		lerArq.close();
		return null;
	}

	private String findEmailID(String linha) {
		String id = null;
		if (linha.contains("X-Comprova-ID:")) {
			int ini = linha.indexOf('_');
			id = linha.substring(ini + 1, linha.length()).trim();
		}
		return id;

	}
}